from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from flask.ext.sqlalchemy import SQLAlchemy
DATABASE = 'flaskr'
DEBUG = True
SECRET_KEY = 'developmen key'
USERNAME = 'assassinpig'
PASSWORD = '123456'
SQLALCHEMY_DATABASE_URI = 'postgresql://assassinpig:123456@localhost/flaskr'

#create our little application
#def create_app():
#    app = Flask(__name__)
#    app.config.from_object(__name__)
#    db = SQLAlchemy()
#    db.init_app(app)
#    return app,db

app = Flask(__name__)
app.config.from_object(__name__)
db = SQLAlchemy(app)
#db.init_app(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)
    password = db.Column(db.String(120))

    def __init__(self, username, email, password):
        self.username = username
        self.email = email
        self.password = password

    def __repr__(self):
        return 'User %r' % self.username

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/login/', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'GET':
        return render_template('login.html')
    elif request.method == 'POST':
        name = request.form['name'] 
        password = request.form['password']
        if User.query.filter(User.username==name, password==password).first() is None:
            error = 'Invalidate username or password' 
        else:
            session['logged_in'] = True 
            flash("You were logged in")
            return redirect(url_for('index'))
    return render_template('login.html', error=error)

@app.route('/logout/')
def logout():
    flash("You were logged out")
    session.pop('logged_in', None)
    return redirect(url_for('index'))

@app.route('/signup/', methods=['GET', 'POST'])
def signup():
    if request.method == 'GET':
        return render_template('signup.html')
    elif request.method == 'POST':
        name = request.form['name'] 
        email = request.form['email']
        password = request.form['password']
        password_confirmation = request.form['password_confirmation']
        if password == password_confirmation:
            user = User(name, email, password)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for('index'))
        else:
            pass

@app.route('/posts/')
def posts_index():
    pass

@app.route('/post/<post_id>')
def show(post_id):
    pass

@app.route('/posts/new/')
def new():
    pass

@app.route('/post/create/', methods=['POST'])
def create():
    pass

@app.route('/post/<post_id>/edit/')
def edit(post_id):
    pass

@app.route('/post/update/', methods=['POST'])
def update():
    pass

@app.route('/post/delete/')
def delete():
    pass

if __name__ == '__main__':
    app.run(host='192.168.0.108')
